package com.tanhua.server.service;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 * @author 陈功炳
 * @since 2021/11/25 20:27
 */
@Service
public class TodayBestService {

    @Autowired
    private UserService userService;

    @Autowired
    private RecommendUserService recommendUserService;

    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询今日佳人
     * @param token
     * @return
     */
    public TodayBest queryTodayBest(String token) {
        //校验token是否有效 通过sso的接口进行校验
        User user = userService.queryUserByToKen(token);
        if (user == null){
            //token非法或已过期
            return null;
        }
        //查询推荐用户（今日佳人）
        TodayBest todayBest = recommendUserService.queryTodayBest(user.getId());
        if (todayBest == null){
            //如果为null就给默认的推荐用户
            todayBest = new TodayBest();
            todayBest.setId(defaultUser);
            todayBest.setFateValue(80L);
        }

        //补全个人信息
        UserInfo userInfo = userInfoService.queryInfoByUserId(todayBest.getId());
        if (userInfo == null){
            return null;
        }
        todayBest.setAge(userInfo.getAge());
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setGender(userInfo.getSex().getValue() == 1 ?"man" : "women");
        todayBest.setTags(userInfo.getTags().split(","));

        return todayBest;
    }

    /**
     * 查询推荐用户
     * @param token
     * @param queryParam
     * @return
     */
    public PageResult queryRecommendation(String token, RecommendUserQueryParam queryParam) {
        //校验token是否有效，通过SSO的接口进行校验
        User user = userService.queryUserByToKen(token);
        if (null == user){
            //token非法或过期
            return null;
        }
        //获取分页
        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());

        PageInfo<RecommendUser> pageInfo = recommendUserService.queryRecommendUserList(user.getId(),queryParam.getPage(),queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if (CollectionUtils.isEmpty(records)){
            //没有查询到推荐的用户列表
            return pageResult;
        }
        //填充个人信息

        //收集推荐用户的id
        Set<Long> userIds = new HashSet<>();
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",userIds);

        List<UserInfo> userInfoList = userInfoService.queryInfoByList(queryWrapper);
        if (CollectionUtils.isEmpty(userInfoList)){
            //没有查询到用户的基本信息
            return pageResult;
        }

        List<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest tb = new TodayBest();
            tb.setId(userInfo.getUserId());
            tb.setAvatar(userInfo.getLogo());
            tb.setNickname(userInfo.getNickName());
            tb.setTags(userInfo.getTags().split(","));
            tb.setGender(userInfo.getSex().getValue() == 1 ? "man": "woman");
            tb.setAge(userInfo.getAge());
            //缘分值
            for (RecommendUser record : records) {
                if (record.getUserId().longValue() == userInfo.getUserId().longValue()){
                    double score = Math.floor(record.getScore());//取整,98.2 -> 98
                    tb.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }
            todayBests.add(tb);
        }
        //按照缘分值进行倒序排序
        Collections.sort(todayBests, (o1, o2) -> new Long(o2.getFateValue() - o1.getFateValue()).intValue());

        pageResult.setItems(todayBests);

        return pageResult;
    }
}
